'''
@Author xianye
@email  123861725@qq.com
@date   2021-10-18
'''
import requests


# 获取api秘钥
def authorize(umeng):
    """
    根据传入的字典型登录信息，登录成功后返回auth_token
    :param umeng: 字典，email,password
    :return: auth_token
    """

    url = 'http://api.umeng.com/authorize'
    response = requests.post(url, params=umeng)
    return response.json()['auth_token']


def apps(auth_token):
    '''
    获取appkey，一般是安卓，苹果两个，如果有多个应用且多种机型，则返回列表。
    :param auth_token:
    :return:
    [{'updated_at': '2021-08-24T13:33:14Z',
      'use_game_sdk': False,
      'name': '宝贝*****',
      'created_at': '2021-08-24T13:33:14Z',
      'appkey': '6124f51a5********59b62e2a',
      'popular': 0,
      'platform': 'iphone'}]
    '''

    url = 'http://api.umeng.com/apps?per_page=10&page=1&auth_token=%s' % (auth_token)
    response = requests.get(url)
    return response.json()


def app_data(appkey, date, auth_token):
    '''
    APP总数据
    :param appkey:
    :param date:
    :param auth_token:
    :return:
    {
    "installations": 186129,总用户数
    "launches": 43799,启动次数
    "date": "2013-01-21",日期
    "active_users": 4317,活跃用户
    "new_users": 189新增用户
    }
    '''

    url = 'http://api.umeng.com/base_data?appkey={}&date={}&auth_token={}'.format(appkey, date, auth_token)
    response = requests.get(url)
    return response.json()


def channels(appkey, auth_token, date=''):
    '''
    获取所有渠道和基本数据,这里的渠道，指的是手机。
    :param appkey:
    :param auth_token:
    :param date:
    :return: [{'date': '2021-10-14',日期
      'duration': '00:01:28',使用时长
      'total_install_rate': 100.0,百分比
      'active_user': 1121,活跃用户
      'install': 45,新增用户
      'total_install': 4140,总用户数
      'channel': 'App Store',渠道名称，也可以称为手机名称
      'launch': 7416,启动数
      'id': '6124f9248c8d*******5616415'}]
    '''

    url = 'http://api.umeng.com/channels?date={}&appkey={}&auth_token={}'.format(date, appkey, auth_token)
    response = requests.get(url)
    return response.json()


def versions(appkey, auth_token, date=''):
    '''
    获取所有版本和基本数据，PS：此段程序感觉没啥鸟用
    :param appkey:
    :param auth_token:
    :param date:'2021-10-14'
    :return: [{'date': '2021-10-14',日期
      'total_install_rate': 62.68,比例
      'active_user': 980,活跃用户
      'install': 44,新增用户
      'total_install': 2580,总新增数
      'version': '3.3.6'}版本号]
    '''

    url = 'http://api.umeng.com/versions?date={}&appkey={}&auth_token={}'.format(date, appkey, auth_token)
    response = requests.get(url)
    return response.json()


def today_data(appkey, auth_token):
    '''
    获取今日数据
    :param appkey:
    :param auth_token:
    :return: {'date': '2021-10-15',日期
     'active_users': 855,活跃用户
     'installations': 4169,总用户数
     'launches': 5067,启动次数
     'new_users': 29}新增用户
    '''

    url = 'http://api.umeng.com/today_data?appkey={}&auth_token={}'.format(appkey, auth_token)
    response = requests.get(url)
    return response.json()


def yesterday_data(appkey, auth_token):
    '''
    获取昨日数据
    :param appkey:
    :param auth_token:
    :return: {'date': '2021-10-15',日期
     'active_users': 855,活跃用户
     'installations': 4169,总用户数
     'launches': 5067,启动次数
     'new_users': 29}新增用户
    '''

    url = 'http://api.umeng.com/yesterday_data?appkey={}&auth_token={}'.format(appkey, auth_token)
    response = requests.get(url)
    return response.json()


def base_data(appkey, auth_token, date):
    '''
    获取指定日期数据
    :param appkey:
    :param auth_token:
    :param date:'2021-10-01'
    :return: {'date': '2021-10-15',日期
     'active_users': 855,活跃用户
     'installations': 4169,总用户数
     'launches': 5067,启动次数
     'new_users': 29}新增用户
    '''

    url = 'http://api.umeng.com/base_data?date={}&appkey={}&auth_token={}'.format(date, appkey, auth_token)
    response = requests.get(url)
    return response.json()


def segmentations(appkey, auth_token):
    '''
    获到用户群，目前已不能用，返回文本结果：byebye
    :param appkey:
    :param auth_token:
    :return: byebye
    '''

    url = 'http://api.umeng.com/segmentations?appkey={}&auth_token={}'.format(appkey, auth_token)
    response = requests.get(url)
    return response


def new_users(appkey, auth_token, start_date, end_date, period_type=''):
    '''
    获取新增用户汇总
    :param appkey:
    :param auth_token:
    :param start_date: '2021-10-01'
    :param end_date: '2021-10-01'
    :param period_type: 日期类型horly,daily,weekly,monthly,默认daily
    :return:
    '''

    url = 'http://api.umeng.com/new_users?appkey={}&start_date={}&end_date={}&auth_token={}&period_type={}'.format(
        appkey, start_date, end_date, auth_token, period_type)
    response = requests.get(url)
    return response


def active_users(appkey, auth_token, start_date, end_date, period_type=''):
    '''
    获取活跃用户汇总
    :param appkey:
    :param auth_token:
    :param start_date:'2021-10-01'
    :param end_date:'2021-10-01'
    :param period_type:日期类型horly,daily,weekly,monthly,默认daily
    :return:
    '''

    url = 'http://api.umeng.com/active_users?appkey={}&start_date={}&end_date={}&auth_token={}&period_type={}'.format(
        appkey, start_date, end_date, auth_token, period_type)
    response = requests.get(url)
    return response


def launches(appkey, auth_token, start_date, end_date, period_type=''):
    '''
    获取活跃用户汇总
    :param appkey:
    :param auth_token:
    :param start_date:
    :param end_date:
    :param period_type:日期类型horly,daily,weekly,monthly,默认daily
    :return:
    '''

    url = 'http://api.umeng.com/launches?appkey={}&start_date={}&end_date={}&auth_token={}&period_type={}'.format(
        appkey, start_date, end_date, auth_token, period_type)
    response = requests.get(url)
    return response


def durations(appkey, auth_token, start_date, end_date, period_type):
    '''
    获取用户使用时长汇总
    :param appkey:
    :param auth_token:
    :param start_date:
    :param end_date:
    :param period_type: 日期类型:日daily，单次daily_per_launch
    :return:
    '''

    url = 'http://api.umeng.com/durations?appkey={}&start_date={}&end_date={}&auth_token={}&period_type={}'.format(
        appkey, start_date, end_date, auth_token, period_type)
    response = requests.get(url)
    return response


def retentions(appkey, auth_token, start_date, end_date, period_type):
    """
    获取用户留存
    :param appkey:
    :param auth_token:
    :param start_date:
    :param end_date:
    :param period_type: 日期类型:日daily，单次daily_per_launch
    :return:
    """

    url = 'http://api.umeng.com/retentions?appkey={}&start_date={}&end_date={}&auth_token={}&period_type={}'.format(
        appkey, start_date, end_date, auth_token, period_type)
    response = requests.get(url)
    return response


def group_list(appkey, auth_token, start_date, end_date):
    """
    获取事件Group列表
    :param appkey:
    :param auth_token:
    :param start_date:
    :param end_date:
    :return:
    """

    url = 'http://api.umeng.com/events/group_list?appkey={}&page=1&per_page=300&start_date={}&end_date={}&auth_token={}'.format(
        appkey, start_date, end_date, auth_token)
    response = requests.get(url)
    return response


def event_list(appkey, auth_token, start_date, end_date, group_id):
    """
    获取事件列表,通过group_list列表，取出事件列表中的每个事件
    :param appkey:
    :param auth_token:
    :param start_date:
    :param end_date:
    :param group_id:
    :return:
    """

    url = 'http://api.umeng.com/events/event_list?appkey={}&start_date={}&end_date={}&auth_token={}&group_id={}'.format(
        appkey, start_date, end_date, auth_token, group_id)
    response = requests.get(url)
    return response


def daily_data(appkey, auth_token, start_date, end_date, group_id):
    """
    获取事件消息数,就是每个事件点进去后，最上面呈现的趋势线上面的消息数据
    :param appkey:
    :param auth_token:
    :param start_date:
    :param end_date:
    :param group_id:
    :return:
    """

    url = 'http://api.umeng.com/events/daily_data?appkey={}&type=count&start_date={}&end_date={}&auth_token={}&group_id={}'.format(
        appkey, start_date, end_date, auth_token, group_id)
    response = requests.get(url)
    return response


def parameter_list(appkey, auth_token, start_date, end_date, event_id):
    """
    参数列表
    返回值：num：计次；label：代购编号；percent：百分比
    :param appkey:
    :param auth_token:
    :param start_date:
    :param end_date:
    :param event_id:
    :return:
    """

    url = 'http://api.umeng.com/events/parameter_list?appkey={}&event_id={}&type=count&start_date={}&end_date={}&auth_token={}'.format(
        appkey, event_id, start_date, end_date, auth_token)
    response = requests.get(url)
    return response


def parameter_data(appkey, auth_token, start_date, end_date, event_id):
    """
    参数列表,莫名其妙，查不到数据
    :param appkey:
    :param auth_token:
    :param start_date:
    :param end_date:
    :param event_id:
    :return:
    """

    url = 'http://api.umeng.com/events/parameter_data?appkey={}&label=navigate&event_id={}&start_date={}&end_date={}&auth_token={}'.format(
        appkey, event_id, start_date, end_date, auth_token)
    response = requests.get(url)
    return response
